﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.Linq;
using WX.WMS.DataModel;
using WX.WMS.Common;
using WX.WMS.BLL;

public partial class InStock_InStockManager : PageBase
{
    private string _inStockID
    {
        get
        {
            if (ViewState["InStockID"] == null)
            {
                ViewState["InStockID"] = "";
            }
            return Convert.ToString(ViewState["InStockID"]);
        }
        set { ViewState["InStockID"] = value; }
    }

    private string _action
    {
        get 
        {
            if (ViewState["Action"] == null)
            {
                ViewState["Action"] = "";
            }
            return Convert.ToString(ViewState["Action"]);
        }
        set { ViewState["Action"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["SortOrder"] = "INSTOCK_DATE";
            ViewState["OrderDire"] = "desc";
            
            Initialization();
        }

    }

    protected void Initialization()
    {
        if (Request.QueryString["Action"] != null)
        {
            this._action = Convert.ToString(Request.QueryString["Action"]);
        }

        DataBandInStockTypeList();
        DataBandStatusList();

        if (this._action == "")
        {
            BindGridView();
        }
        else if (this._action.ToString().Equals("del"))
        {
            if (Request.QueryString["ID"] != null)
            {
                this._inStockID = Convert.ToString(Request.QueryString["ID"]);
            }

            InStockBLL inStockBLL = new InStockBLL();

            int status = inStockBLL.GetInStockByID(this._inStockID).STATUS;

            if (status == (int)Enumerations.StockStatus.审核通过)
            {
                ShowMessage("此单据已被审核，不能删除！");
                return;
            }

            if (inStockBLL.DeleteInStock(this._inStockID) == 1)
            {
                ShowMessage("删除成功！");
                BindGridView();
                return;
            }
            else
            {
                ShowMessage("删除失败！请重试！");
                return;
            }
        }

        this.InStockDateFrom.Attributes["readonly"] = "readonly";
        this.InStockDateTo.Attributes["readonly"] = "readonly";
    }

    protected void BindGridView()
    {
        InStockBLL bll = new InStockBLL();

        if (this.CreateUser.Text.Trim() != "")
        {
            bll.AddLikeCondition("T_SYS_USER1.NAME", this.CreateUser.Text.Trim());
        }
        if (this.InStockNo.Text.Trim() != "")
        {
            bll.AddLikeCondition("INSTOCK_ID", this.InStockNo.Text.Trim());
        }
        if (this.InStockDateFrom.Text.Trim() != "" && this.InStockDateTo.Text.Trim() != "")
        {
            bll.AddBetweenCondition("INSTOCK_DATE", this.InStockDateFrom.Text.Trim(), this.InStockDateTo.Text.Trim(), true);
        }
        if (this.ClientName.Text.Trim() != "")
        {
            bll.AddLikeCondition("T_CLIENT.NAME", this.ClientName.Text.Trim());
        }
        if (this.ddlInStockType.SelectedValue != "-1")
        {
            bll.AddEqualCondition("TYPE", this.ddlInStockType.SelectedValue);
        }
        if (this.ddlStatus.SelectedValue != "-1")
        {
            bll.AddEqualCondition("STATUS", this.ddlStatus.SelectedValue);
        }

        bll.AddOrderByCondition(ViewState["SortOrder"] + " " + ViewState["OrderDire"]);

        this.gvInStockList.DataSource = bll.GetInStockList();
        this.gvInStockList.DataBind();
    }

    protected void gvInStockList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gvInStockList.PageIndex = e.NewPageIndex;
        BindGridView();
    }
    protected void gvInStockList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string ID = gvInStockList.DataKeys[e.RowIndex]["ID"].ToString();
        if (new InStockBLL().IsAudited(ID))
        {
            ShowMessage("审核通过的不能删除");
        }
        else {
            Response.Redirect("InStockManager.aspx?Action=del&ID="+ID);
        }
    }
    protected void gvInStockList_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string ID = gvInStockList.DataKeys[e.NewEditIndex]["ID"].ToString();
        if (new InStockBLL().IsAudited(ID))
        {
            ShowMessage("审核通过的不能编辑");
        }
        else
        {
            Response.Redirect("InStockDetail.aspx?Action=edit&ID=" + ID);
        }
    }

    protected void DataBandInStockTypeList()
    {
        CommomMethod.BindEnumForDropdwonList(this.ddlInStockType, typeof(Enumerations.InStockType));
        this.ddlInStockType.Items.Insert(0, new ListItem("请选择", "-1"));
    }

    protected void DataBandStatusList()
    {
        CommomMethod.BindEnumForDropdwonList(this.ddlStatus, typeof(Enumerations.StockStatus));
        this.ddlStatus.Items.Insert(0, new ListItem("请选择", "-1"));
    }

    protected void searchButton_Click(object sender, ImageClickEventArgs e)
    {
        BindGridView();
    }
}
